import {queryAdmin,getRadioStationByRsId,saveOrUpdateRadioStation,saveOrUpdateAccount,delectAccount,validateAccountOnly } from '../services/api';

export default {
  namespace: 'admin',

  state: {
    loading: true,
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *getAdmin({ payload,callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryAdmin, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    },

    *getRadioStationByRsId({ payload,callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getRadioStationByRsId, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    },

    *saveOrUpdateRadioStation({ payload,callback }, { call, put }) {
      const response = yield call(saveOrUpdateRadioStation, payload);
      if (callback) callback(response);
    },

    *saveOrUpdateAccount({payload ,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(saveOrUpdateAccount,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    },

    *delectAccount({payload ,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(delectAccount,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    },

    *validateAccountOnly({payload ,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(validateAccountOnly,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    },




  },

  reducers: {
    query(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },


  },
};
